/************************************************************************************************
 *   深圳市摩西尔电子有限公司 @版本所有@
 *
 *   此文件用于连接屏数据存储转换
 *
 * 修改:
 *   1. 类型 : 创建
 *      作者 : 李智明
 *      时间 : 2020.3.7
 *      内容 : 所有代码
 *************************************************************************************************/
/* exported mc_sdk_screen */

function mc_sdk_screen() {
    /*     var xml_display_count = "DISPLAY_COUNT";
        var xml_display_idx = "DISPLAY_IDX";
        var xml_row_count = "ROW_COUNT";
        var xml_col_count = "COL_COUNT";
        var xml_x_offset = "X_OFFSET";
        var xml_y_offset = "Y_OFFSET";
        var xml_network_count = "NETWORK_COUNT";
        var xml_network = "NETWORK";
        var xml_network_data_count = "NETWORK_DATA_COUNT";
        var xml_vcs_addr = "VCS_ADDR";
        var xml_vcs_width = "VCS_WIDTH";
        var xml_vcs_height = "VCS_HEIGHT";
        var xml_row = "ROW";
        var xml_col = "COL";

        var str_display_count = "";
        var str_display_idx = ""; */
    var str_row_count = "";
    var str_col_count = "";
    // var str_network = "";
    /*     var str_x_offset = "";
        var str_y_offset = "";
        var str_network_count = "";
        var str_network_data_count = "";
        var str_vcs_addr = "";
        var str_vcs_width = "";
        var str_vcs_height = "";
        var str_row = "";
        var str_col = ""; */

    // network组数
    var item_network_count = "";

    //总数据
    var all_date = "";

    this.mc_screen_get_xml = function (date) {
        if (!date) {
            return false;
        }
        all_date = date;

        // 行列
        str_row_count = date.table_line;
        str_col_count = date.table_column;

        //网口序号
        // str_network = date.data[0].network;

        item_network_count = date.data.length;
        var k = 0;
        var str_xml_data = "";

        str_xml_data += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        str_xml_data += "<DISPLAY_CONNECT_INFO version=\"1.0\">\n";
        str_xml_data += "<BASIC_INFO display_count=\"2\">\n";

        for (var i = 0; i < item_network_count; i++) {
            str_xml_data += "<DISPLAY display_idx=\"0\" row_count=\"" + str_row_count + "\" col_count=\"" + str_col_count + "\" x_offset=\"0\" y_offset=\"0\" network_count=\"" + all_date.data.length + "\">\n";

            str_xml_data += "<DATA_ITEM network=\"" + all_date.data[i].network + "\" network_data_count=\"" + all_date.data[i].network_data.length + "\" >\n";
            for (var j = 0; j < all_date.data[i].network_data.length; j++) {
                k++;
                str_xml_data += "<ITEM  vcs_addr=\"" + k + "\" vcs_width=\"" + all_date.data[i].network_data[j].split(",")[3] + "\" vcs_height=\"" + all_date.data[i].network_data[j].split(",")[4] + "\" row=\"" + all_date.data[i].network_data[j].split(",")[1] + "\" col=\"" + all_date.data[i].network_data[j].split(",")[0] + "\"/>\n";
            }
            k = 0;
            str_xml_data += "</DATA_ITEM>\n";
            str_xml_data += "</DISPLAY>\n";
        }

        str_xml_data += "</DISPLAY_CONNECT_INFO>";

        return str_xml_data;
    };

    this.mc_screen_set_val = function (date) {
        if (!date) {
            return false;
        }
        // 判断是否为乱码
        var Pattern = new RegExp("[�ǣ]");

        if (Pattern.test(date)) {
            return false;
        }
        var obj_date = {
            "table_line": "",
            "table_column": "",
            data: []
        };


        // col_val,row_val,vcs_addr_val,vcs_width_val,vcs_height_val

        var cla_xml_doc = new DOMParser();
        var cla_doc = cla_xml_doc.parseFromString(date, "text/xml");

        //获取行列
        var cla_DISPLAY = cla_doc.getElementsByTagName("DISPLAY")[0];

        if (!cla_DISPLAY) {
            return false;
        }
        //行列
        var get_table_line_val = cla_DISPLAY.getAttribute("row_count");
        var get_table_col_val = cla_DISPLAY.getAttribute("col_count");

        obj_date.table_line = get_table_line_val;
        obj_date.table_column = get_table_col_val;

        //网口数量
        var get_table_network_count = cla_DISPLAY.getAttribute("network_count");


        for (var i = 0; i < get_table_network_count; i++) {
            var obj_inner_date = {
                "network": "",
                "network_data": []
            };
            // 网口号
            var get_network = cla_doc.getElementsByTagName("DATA_ITEM")[i].getAttribute("network");

            if (0 > Number(get_network) || 16 < Number(get_network)) {
                return false;
            }

            //网口数量
            var get_network_data_count = cla_doc.getElementsByTagName("DATA_ITEM")[i].getAttribute("network_data_count");


            for (var j = 0; j < get_network_data_count; j++) {
                var get_col_val = cla_doc.getElementsByTagName("DATA_ITEM")[i].getElementsByTagName("ITEM")[j].getAttribute("col");
                var get_row_val = cla_doc.getElementsByTagName("DATA_ITEM")[i].getElementsByTagName("ITEM")[j].getAttribute("row");
                var get_vcs_addr_val = cla_doc.getElementsByTagName("DATA_ITEM")[i].getElementsByTagName("ITEM")[j].getAttribute("vcs_addr");
                var get_vcs_width_val = cla_doc.getElementsByTagName("DATA_ITEM")[i].getElementsByTagName("ITEM")[j].getAttribute("vcs_width");
                var get_vcs_height_val = cla_doc.getElementsByTagName("DATA_ITEM")[i].getElementsByTagName("ITEM")[j].getAttribute("vcs_height");
                var str_each_date = get_col_val + "," + get_row_val + "," + get_vcs_addr_val + "," + get_vcs_width_val + "," + get_vcs_height_val;

                obj_inner_date.network_data.push(str_each_date);
            }


            obj_inner_date.network = get_network;
            obj_date.data.push(obj_inner_date);
            // console.log(obj_date.data[i].network);
        }
        return obj_date;
    };
}
